﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseTypesLib.Interfaces;
using System.Runtime.Serialization;
using BaseTypesControlLib.Controls;
using System.Xml.Linq;

namespace BaseTypesLib.Model
{

    [DataContract(IsReference = true)]
    public class DateField:Field, IEditable, IReadOnly, IDrawStyle
    {
        private bool readOnly, editable = true;
        private string label;
        private DateTime date;
        private DateFieldMode mode;
        private TimeFormat tformat;
        private DateFormat dformat;

        public DateField()
        {
            date = DateTime.Now;
            mode = DateFieldMode.DateTime;
            TimeFormat = TimeFormat.Default;
            DateFormat = DateFormat.Default;
        }

        [DataMember]
        public string Label
        {
            get { return label; }
            set
            {
                if (label != value)
                {
                    label = value;
                    OnPropertyChanged("Label");
                }
            }
        }
        [DataMember]
        public bool ReadOnly
        {
            get { return readOnly; }
            set
            {
                if (readOnly != value)
                {
                    readOnly = value;
                    OnPropertyChanged("ReadOnly");
                }
            }
        }

        [DataMember]
        public bool Editable
        {
            get { return editable; }
            set
            {
                if (editable != value)
                {
                    editable = value;
                    OnPropertyChanged("Editable");
                }
            }
        }

        [DataMember]
        public DateTime Date
        {
            get { return date; }
            set
            {
                if (date != value)
                {
                    date = value;
                    OnPropertyChanged("Date");
                }
            }
        }

        [DataMember]
        public DateFieldMode Mode
        {
            get { return mode; }
            set
            {
                if (mode != value)
                {
                    mode = value;
                    OnPropertyChanged("Mode");
                }
            }
        }

        [DataMember]
        public TimeFormat TimeFormat
        {
            get { return tformat; }
            set
            {
                if (tformat != value)
                {
                    tformat = value;
                    OnPropertyChanged("TimeFormat");
                }
            }
        }

        [DataMember]
        public DateFormat DateFormat
        {
            get { return dformat; }
            set
            {
                if (dformat != value)
                {
                    dformat = value;
                    OnPropertyChanged("DateFormat");
                }
            }
        }
        protected override XElement GetXmlElement()
        {
            XElement el = base.GetXmlElement();

            if (Label != null && Label.Trim().Length > 0)
                el.SetAttributeValue("Label", Label.Trim());
            el.SetAttributeValue("Date", Date);
            el.SetAttributeValue("Mode", Mode);
            el.SetAttributeValue("DateFormat", DateFormat);
            el.SetAttributeValue("TimeFormat", TimeFormat);
            if (ReadOnly)
                el.SetAttributeValue("ReadOnly", ReadOnly);
            if (!Editable)
                el.SetAttributeValue("Editable", Editable);

            return el;
        }
        protected override void FromXmlElement(XElement elem)
        {
            base.FromXmlElement(elem);
            var lbl = elem.Attributes("Label").FirstOrDefault();
            if (lbl != null)
                Label = lbl.Value;

            var date = elem.Attributes("Date").FirstOrDefault();
            if (date != null) Date = (DateTime)date;
            var mode = elem.Attributes("Mode").FirstOrDefault();
            if (mode != null) Mode = (DateFieldMode)Enum.Parse(typeof(DateFieldMode), mode.Value);

            var df = elem.Attributes("DateFormat").FirstOrDefault();
            if (df != null) DateFormat = (DateFormat)Enum.Parse(typeof(DateFormat), df.Value);
            var tf = elem.Attributes("TimeFormat").FirstOrDefault();
            if (tf != null) TimeFormat = (TimeFormat)Enum.Parse(typeof(TimeFormat), tf.Value);

            var ro = elem.Attributes("ReadOnly").FirstOrDefault();
            if (ro != null) ReadOnly = (bool)ro;
            var e = elem.Attributes("Editable").FirstOrDefault();
            if (e != null) Editable = (bool)e;
        }
    }
}
